package cn.ssms.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 * @author SDDM
 *
 */
public class DateUtil {
	static DateFormat dateFormatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/**
	 * yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getNowDate(){
		try {
			return dateFormatter.format(new Date());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * str 格式为yyyy-MM-dd HH:mm:ss
	 * @param str
	 * @return
	 */
	public static Date String2Date(String str){
		try {
			return dateFormatter.parse(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 几秒后的时间
	 * @param second
	 * @return
	 */
	public static String getAfterSecond(int second){
		try{ 
		    SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar c = Calendar.getInstance();  
			Date date =new Date();		
			System.out.println(formatDate.format(date));
			c.setTimeInMillis(date.getTime());
			c.add(Calendar.SECOND, second);//几秒后
			System.out.println(formatDate.format(new Date(c.getTimeInMillis())));
			return  formatDate.format(new Date(c.getTimeInMillis()));
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 当前时间后几秒的时间
	 * @param parseInt
	 * @return
	 */
	public static Date AfterSSDate(int second) {
		try{
			Date dNow = new Date();   //当前时间
			Calendar calendar = Calendar.getInstance(); //得到日历
			calendar.setTime(dNow);//把当前时间赋给日历
			calendar.add(Calendar.SECOND, second); 
			System.out.println(calendar.getTime());
			return calendar.getTime();   
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 当前几个月后的时间
	 * @param life
	 * @return
	 */
	public static String AfterMonthDate(Integer life) {
		try{ 
		    SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar c = Calendar.getInstance();  
			Date date =new Date();		
			System.out.println(formatDate.format(date));
			c.setTimeInMillis(date.getTime());
			c.add(Calendar.MONTH, life);//几秒后
			System.out.println(formatDate.format(new Date(c.getTimeInMillis())));
			return  formatDate.format(new Date(c.getTimeInMillis()));
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 后几天的时间
	 * @param date
	 * @param day
	 * @return
	 */
	public static String AfterDateTime(String dateStr, int day) {
		try{ 
		    SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar c = Calendar.getInstance();  		
			c.setTimeInMillis(dateFormatter.parse(dateStr).getTime());
			c.add(Calendar.DATE, day);//几天后			
			return  formatDate.format(new Date(c.getTimeInMillis()));
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

}
